package com.mentorgen.tools.profile.output;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.mentorgen.tools.profile.Controller;
import com.mentorgen.tools.profile.runtime.EntityDictionary;
import com.mentorgen.tools.profile.runtime.Method;
/**
 * This class dumps the collected info from Method objects into a DOT object to enable drawing a graph
 * 
 * @author nima
 *
 */

public abstract class ProfileDotDump {
	
	static final String DEFUALT_BNAME = "UNKNOWN";
	static Map<String, Long> dataMap; 
	static Map<String, Long> countMap; 
	
	static PrintWriter logWriter = null;
	
	static void dump() throws IOException {
		
		ProfileDotDump.dump("com.mentorgen.tools.profile.output.ProfileBundleDotDump", "graph_bundle");
//		ProfileDotDump.dump("com.mentorgen.tools.profile.output.ProfileClassDotDump", "graph_class");
	}

	static void dump(String clazz, String dumpFileName) throws IOException {
		
		dataMap = new HashMap<String, Long>();
		countMap = new HashMap<String, Long>();
		
		PrintWriter bundleFileWriter = new PrintWriter(new File(dumpFileName + ".txt"));
		
		Map<String, Method> methodDic = EntityDictionary.getMethodDic();
		
		bundleFileWriter.write("digraph Rubis {\n");
		
		try{
			
			if (Controller._debug)
				logWriter = new PrintWriter(new File("MTrace_"+ dumpFileName + ".txt"));
			
			IFProfileDotDump profileDotDump = (IFProfileDotDump) Class.forName(clazz).newInstance();
			
			profileDotDump.writeNodes(bundleFileWriter, methodDic);
			profileDotDump.writeEdges(bundleFileWriter, methodDic);
		
			bundleFileWriter.write("}\n");
			bundleFileWriter.flush();
			bundleFileWriter.close();
			
			// TODO nimak - disabled dumping to a png file for the cloud
//			profileDotDump.dumpGraph(dumpFileName + ".txt", dumpFileName + ".png");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			
			if (Controller._debug){
				if (logWriter == null) return;
				logWriter.flush();
				logWriter.close();
			}
		}
	}
	
	protected static synchronized void writeDotNodes(PrintWriter fileWriter, Set<String> set){
		
		fileWriter.write("		node [weight=1, height=1, color=lightblue2, style=filled];\n");
		for (String methodName : set)
			fileWriter.write("		" + normalizeName(methodName) +";\n");
	}
	
	protected static synchronized void writeDotEdges(PrintWriter fileWriter){
		
		for (String jointBundle : dataMap.keySet()){
			
			System.err.println(jointBundle);
			String[] bundles = jointBundle.split("___");
			fileWriter.write("	    " + normalizeName(bundles[0]) + " -> " + normalizeName(bundles[1]) + 
					" [ label = \" " + dataMap.get(jointBundle) + 
					"__" + countMap.get(jointBundle) + "\" ];\n");
		}
	}
	
	protected static String normalizeName(String name){
		return name.replace("-", "_").replace(".", "_").replace(":", "_").replace("$", "_").replace("/", "_").replace("\\", "_");
	}

}
